package com.kotlin.myapplication.app

import android.app.ActivityManager
import android.content.Context
import androidx.multidex.MultiDexApplication
import com.kotlin.myapplication.utils.Logger


/**
 *@lil
 *@创建时间 2019/11/4 9:37
 *@描述
 */
class App : MultiDexApplication() {

    object app {
        lateinit var context: Context
    }


    override fun onCreate() {
        super.onCreate()
        init()
    }

    private fun init() {
        app.context = getApplicationContext();
        Logger.log("1111111111111")
        // 判断当前进程是否是本进程 进程会调用多次 创建多个对象
        var processName = getProcessName(this, android.os.Process.myPid())
        if (null != processName) {
            // 判断当前的进程是我们自己app
            var myPid = processName.equals("com.kotlin.myapplication")
            if (myPid) {
                // 当前应用的初始化
            }
        }

    }

    private fun getProcessName(cxt: Context, pid: Int): String? {

        var manager = cxt.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val runningApps = manager.getRunningAppProcesses() ?: return null
        for (procInfo in runningApps!!) {
            if (procInfo.pid == pid) {
                return procInfo.processName
            }
        }
        return null
    }


    companion object {
        fun getContext(): Context? {
            return app.context
        }
    }


}
